import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/entity/company_entity.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'account_manage_controller.dart';

class AccountManagePage extends StatelessWidget {
  final controller = Get.put(AccountManageController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(
        context: context,
        title: "账户管理",
        actions: [
          IconButton(
            onPressed: () {
              controller.searchAccount();
            },
            icon: Image.asset("images/ic_search_black.png"),
          ),
          IconButton(
            onPressed: () {
              controller.addAccount();
            },
            icon: Image.asset("images/ic_add_account.png"),
          ),
        ],
      ),
      body: GetBuilder<AccountManageController>(
        builder: (controller) {
          return MultiStateWidget(
            child: ListView.builder(
              itemCount: controller.companyList.length,
              shrinkWrap: true,
              physics: BouncingScrollPhysics(),
              controller: controller.scrollController,
              itemBuilder: (context, index) {
                return buildItemWidget(index);
              },
            ),
            state: controller.multiState,
            onRetry: () {
              controller.onRetry();
            },
          );
        },
      ),
    );
  }

  Widget buildItemWidget(int index) {
    CompanyEntity companyEntity = controller.companyList[index];
    bool isNotSettlement = companyEntity.isNotSettlement ?? false;
    return Container(
      margin: EdgeInsets.only(left: 16.w, top: index == 0 ? 12.w : 0, right: 16.w, bottom: 12.w),
      decoration: BoxDecoration(
        color: Colours.color_white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.w),
        child: Material(
          child: InkWell(
            onTap: () {},
            child: Padding(
              padding: EdgeInsets.only(left: 16.w, top: 13.w, right: 16.w, bottom: 8.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    companyEntity.name ?? "",
                    style: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(
                    height: 3.w,
                  ),
                  Text(
                    companyEntity.code ?? "",
                    style: TextStyle(
                      color: Colours.color_b1b2bb,
                      fontSize: 12.sp,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(
                    height: 9.w,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      TextButtonWithIcon(
                        onPressed: () {
                          controller.setSettlementState(companyEntity.id ?? "", isNotSettlement);
                        },
                        icon: Image.asset("images/ic_${isNotSettlement ? "selected" : "unselect"}.png"),
                        label: Text(
                          "不再结算",
                          style: TextStyle(color: Colours.color_text_theme, fontSize: 12.sp),
                        ),
                        style: ButtonStyle(
                          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                          minimumSize: MaterialStateProperty.all(Size(0, 0)),
                          padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                        ),
                        textIconPadding: 5.w,
                      ),
                      SizedBox(
                        width: 10.w,
                      ),
                      Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Visibility(
                            visible: !isNotSettlement,
                            child: TextButtonWithIcon(
                              onPressed: () {
                                controller.backResult(companyEntity.id ?? "", companyEntity.name ?? "", companyEntity.code ?? "");
                              },
                              icon: Image.asset("images/ic_bar_chart.png"),
                              label: Text(
                                "公司结算",
                                style: TextStyle(
                                  color: Colours.color_6f7082,
                                  fontSize: 14.sp,
                                ),
                              ),
                              style: ButtonStyle(
                                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                                backgroundColor: MaterialStateProperty.all(Colours.color_white),
                                shape: MaterialStateProperty.all(
                                  RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(6.w),
                                    side: BorderSide(width: 1.w, color: Colours.color_e2e2d5),
                                  ),
                                ),
                                minimumSize: MaterialStateProperty.all(Size(99.w, 30.w)),
                              ),
                              textIconPadding: 5.w,
                            ),
                          ),
                          SizedBox(
                            width: 8.w,
                          ),
                          TextButtonWithIcon(
                            onPressed: () {
                              Get.toNamed(Routes.cashFlow, arguments: companyEntity.id ?? "");
                            },
                            icon: Image.asset("images/ic_money.png"),
                            label: Text(
                              "资金流水",
                              style: TextStyle(
                                color: Colours.color_6f7082,
                                fontSize: 14.sp,
                              ),
                            ),
                            style: ButtonStyle(
                              tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              backgroundColor: MaterialStateProperty.all(Colours.color_white),
                              shape: MaterialStateProperty.all(
                                RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(6.w),
                                  side: BorderSide(width: 1.w, color: Colours.color_e2e2d5),
                                ),
                              ),
                              minimumSize: MaterialStateProperty.all(Size(99.w, 30.w)),
                            ),
                            textIconPadding: 5.w,
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
